let app = getApp();
let testAddr = app.globalData.testAddr;
Page({
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function () {
    let that=this;
    app.util.request({
      url: testAddr + "address/selectProvince",
      method: "POST",
      success: function (res) {
        let citys = res.data;
        let cityAZ = that.data.cityAZ;
        let newAz=[];
        let allCitys={}
        let cityResults=[];
        
        for (let i in cityAZ){
          allCitys[cityAZ[i].cityName]=[];
          for (let j in citys){
            if(citys[j].description===cityAZ[i].cityName){
              allCitys[cityAZ[i].cityName].push({
                provinceName: citys[j].name,
                provinceCode: citys[j].code,
                description: citys[j].description
              });
            }
          }
        }
        for(let i in allCitys){
          let provinceCitys=allCitys[i];
          if (provinceCitys.length>0){
            cityResults.push({ name: i, code: "", description: "" });
            for (let j in provinceCitys) {
              cityResults.push({ name: provinceCitys[j].provinceName, code: provinceCitys[j].provinceCode, description: provinceCitys[j].description });
            }
            newAz.push({cityName:i});
          }
        }
        that.setData({
          cityResults: cityResults,
          cityAZ:newAz,
          citys: cityResults
        })
      },
      fail: function (res) {
        
      },
      complete: function (res) {

      },
    });
    
  },
  bindAZ: function (e) {
    var currentCityName = e.currentTarget.dataset.id
    var that = this;
    //放入A-Z的scrollTop参数
    wx.createSelectorQuery().selectAll('.city-item-A-Z').fields({
      dataset: true,
      size: true,
      rect: true
    }, function (res) {
      res.forEach(function (re) {
        if (currentCityName == re.dataset.cityname) {
          wx.pageScrollTo({
            scrollTop: re.top + that.data.scrollNow - 55.5,
            duration: 0
          })
        }
      })
    }).exec();
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    this.setData({
      scrollNow: e.scrollTop
    })
  },

  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {

  },
  citySelected: function (e) {
    var cityNameTemp = e.currentTarget.dataset.cityname;
    var cityCode = e.currentTarget.dataset.citycode;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    if (cityNameTemp){
      prevPage.setData({//直接给上移页面赋值
        province: cityNameTemp,
      });
    }
    if (cityCode){
      prevPage.setData({//直接给上移页面赋值
        provinceID: cityCode,
      });
    }
    prevPage.queryProducts();
    wx.navigateBack({ delta: 1})
  },
  bindSarchInput: function (e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })

    var inputVal = e.detail.value;
    var cityResultsTemp = new Array()
    var citys = this.data.citys;

    if (inputVal == null || inputVal.trim() == '') {
      this.setData({
        cityResults: citys
      })
      return;
    }

    for (var i = 0; i < citys.length; i++) {
      console.log(citys[i].name.indexOf(inputVal));
      if (citys[i].name.indexOf(inputVal)>-1) {
          cityResultsTemp.push(citys[i]);
      }
    }
    this.setData({
      cityResults: cityResultsTemp
    })
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {

  },

  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {
    
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 1000)

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }, /**
 * 页面的初始数据
 */
  data: {
    scrollAZ: null,
    scrollNow: 0,
    cityType: 'begin',
    cityResults: null,
    cityAZ: [{ cityName: 'A' }, { cityName: 'B' }, { cityName: 'C' }, { cityName: 'D' }, { cityName: 'E' }, { cityName: 'F' }, { cityName: 'G' }, { cityName: 'H' }, { cityName: 'J' }, { cityName: 'K' }, { cityName: 'L' }, { cityName: 'M' }, { cityName: 'N' }, { cityName: 'P' }, { cityName: 'Q' }, { cityName: 'R' }, { cityName: 'S' }, { cityName: 'T' }, { cityName: 'W' }, { cityName: 'X' }, { cityName: 'Y' }, { cityName: 'Z' },],
    citys: []
  }
})